package model

import (
	"gorm.io/gorm"
	"iqiyi-srv/basic/config"
	__ "iqiyi-srv/basic/proto"
	"time"
)

type Video struct {
	Id           uint32         `gorm:"column:id;type:int UNSIGNED;comment:ID;primaryKey;not null;" json:"id"`                          // ID
	Title        string         `gorm:"column:title;type:varchar(60);comment:视频标题;not null;" json:"title"`                              // 视频标题
	Info         string         `gorm:"column:info;type:varchar(150);comment:内容简介;not null;" json:"info"`                               // 内容简介
	CateTag      string         `gorm:"column:cate_tag;type:varchar(30);comment:分类标签;not null;" json:"cate_tag"`                        // 分类标签
	VipStatus    int8           `gorm:"column:vip_status;type:tinyint(1);comment:观看权限 0-免费 1-vip专享 2-付费点播;not null;" json:"vip_status"` // 观看权限 0-免费 1-vip专享 2-付费点播
	Image        string         `gorm:"column:image;type:varchar(300);comment:封面/海报;not null;" json:"image"`                            // 封面/海报
	VideoUrl     string         `gorm:"column:video_url;type:varchar(300);comment:视频URL;not null;" json:"video_url"`                    // 视频URL
	Status       int8           `gorm:"column:status;type:tinyint(1);comment:视频状态 0-待审 1-已通过 2-下架/驳回;not null;" json:"status"`          // 视频状态 0-待审 1-已通过 2-下架/驳回
	VideoListId  int32          `gorm:"column:video_list_id;type:int;comment:剧集列表ID;not null;" json:"video_list_id"`                    // 剧集列表ID
	LikeCount    int32          `gorm:"column:like_count;type:int;comment:点赞量;not null;default:0;" json:"like_count"`                   // 点赞量
	CommentCount int32          `gorm:"column:comment_count;type:int;comment:评论量;not null;default:0;" json:"comment_count"`             // 评论量
	CreatedAt    time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt    time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt    gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (v *Video) TableName() string {
	return "video"
}

// 发布
func (v *Video) AddVideo() error {
	return config.DB.Create(&v).Error
}

// 分页
func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}

//db.Scopes(Paginate(r)).Find(&users)

// 筛选
func (v *Video) KeywordVideo(keyword string) (list []*__.VideoList, err error) {
	err = config.DB.Model(Video{}).
		Select("title,info,cate_tag,image,video_url").
		Where("cate_tag = ?", keyword).
		Find(&list).Error
	return
}

// 详情
func (v *Video) InfoVideo(videoId int) (list *__.VideoInfo, err error) {
	err = config.DB.Model(Video{}).
		Select("title,info,cate_tag,image,video_url,video_list_id,like_count").
		Where("id = ?", videoId).
		Find(&list).Error
	return
}

type VideoList struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;comment:ID;primaryKey;not null;" json:"id"`          // ID
	Status    int8           `gorm:"column:status;type:tinyint(1);comment:剧集状态 0-未完结 1-已完结;not null;" json:"status"` // 剧集状态 0-未完结 1-已完结
	Title     string         `gorm:"column:title;type:varchar(60);comment:剧集名称;not null;" json:"title"`              // 剧集名称
	Count     int32          `gorm:"column:count;type:int;comment:剧集数;not null;" json:"count"`                       // 剧集数
	CreatedAy time.Time      `gorm:"column:created_ay;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_ay"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (l *VideoList) TableName() string {
	return "video_list"
}

type Protobuf struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;comment:ID;primaryKey;not null;" json:"id"`                // ID
	VideoId   int32          `gorm:"column:video_id;type:int;comment:视频ID;not null;" json:"video_id"`                      // 视频ID
	Status    int8           `gorm:"column:status;type:tinyint(1);comment:审核状态 0-待审 1-通过 2-驳回/下架;not null;" json:"status"` // 审核状态 0-待审 1-通过 2-驳回/下架
	Comment   string         `gorm:"column:comment;type:varchar(60);comment:审核备注/原因;not null;" json:"comment"`             // 审核备注/原因
	CreatedAy time.Time      `gorm:"column:created_ay;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_ay"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (p *Protobuf) TableName() string {
	return "protobuf"
}

// 添加
func (p *Protobuf) AddProto() error {
	return config.DB.Create(&p).Error
}

// 审核
func (p *Protobuf) UpdateProto() error {
	return config.DB.Where("video_id = ?", p.VideoId).Updates(&p).Error
}
